<?php
/**
 * Handles all contact with the Straight Street API
 *
 *
 * @author     Sebastian Skuse <scs@ecs.soton.ac.uk>
 * @copyright  2009-2010 University of Southampton
 * @license    http://<LICENSE-URL>  <LICENSE-NAME>
 * @version    CVS: $1:$
 */

// Require the abstract class apiCall
require_once(SYS_CLASSDIR . "/API/apiCall.abstract.php");


class svcStraightStreet extends apiCall {
	// Static properties. These can be accessed from outside the class without invoking.
	public static $_visibility = "global"; // global, private, group
	public static $required_arguments = array("language", "method", "term");
	public static $apiName = "Mulberry Symbols"; // The name of this API.

	// Properties use while querying the API.
	private $_requestURL;
	private $_requestType;
	private $_args = array("appid" => "PicBoard");
	private $_queryArgs = array("appid", "page", "pagesize");
	
	private $baseURL = "http://straight-street.com/api/";
	


	public function __construct(array $arguments){
		// Check to see if we have been provided the correct set of arguments.
		if( count(array_intersect_key( self::getArguments(), array_keys($arguments))) !== count(self::getArguments())) throw new PicBoardException("Required arguments were not supplied to __costruct. Required arguments for this service are: " . implode(", ", self::getArguments()) . ". Arguments provided: " . implode(", ", array_keys($arguments)) );
		
		$this->_args = array_merge($this->_args, $arguments);
		$this->buildSearch();
	}

	
	private function buildSearch(){
		$this->_args['term'] = urlencode(trim($this->_args['term']));
		
		if($this->_args['term'] == "") throw new PicBoardException("Null term provided to " . __CLASS__ . "->buildSearch.");
	
		// Builds URL with format http://straight-street.com/api/LANG/TERM?q
		$queryArray = array();
		
		// All the arguments that we need to send via a query string, extract from $this->_args.
		foreach($this->_queryArgs as $queryKey){
			if(array_key_exists($queryKey, $this->_args)) $queryArray[$queryKey] = $this->_args[$queryKey];
		}
		
		// Set the queryArray back to the global _queryArgs property.
		$this->_queryArgs = $queryArray;
		
		// Set the _requestURL to the full absolute URL to the webservice.
		$this->_requestURL = $this->baseURL . $this->_args['method'] . "/" . strtoupper($this->_args['language']) . "/" . $this->_args['term'] . "?" . http_build_query($this->_queryArgs);
	}
	
	public function search(){
		$this->makeRequest($this->_requestURL, "json");
	}
	

	// Abstract methods that all API classes must have.
	public function getResults(){
		return $this->getRAW();
	}
	
	public static function getName(){
		return self::$apiName;
	}
	
	public static function getVisibility(){
		return self::$_visibility;
	}
	
	public static function getArguments(){
		// This method returns an array of all the arguments that are required for this API class to function correctly.
		return self::$required_arguments;
	}

}

?>